package com.zhangyang.esper.epl;

import com.espertech.esper.client.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Dell
 * @Title:
 * @Description: 请写注释类
 * @date 2023/3/29
 */
public class DateComparisonDemo {
    public static void main(String[] args) {
        // 注册事件类型
        Configuration configuration = new Configuration();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", String.class);
//        map.put("timestamp", Date.class);
        configuration.addEventType("demo",map);

        // 创建 Esper 引擎
        EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider(configuration);




        // 注册监听器
        epService.getEPAdministrator().createEPL("select * from demo where id = 'event1'")
                .addListener(new DateComparisonListener());

        // 发送事件
        EPRuntime runtime = epService.getEPRuntime();
        runtime.sendEvent(createEvent("event1", new Date()));
        runtime.sendEvent(createEvent("event2", new Date(1640995200000L))); // 2022-01-01T00:00:00Z
    }

    private static Map<String, Object> createEvent(String id, Date timestamp) {
        Map<String, Object> event = new HashMap<>();
        event.put("id", id);
        event.put("timestamp", timestamp);
        return event;
    }

    public static class DateComparisonListener implements UpdateListener {
        @Override
        public void update(EventBean[] newEvents, EventBean[] oldEvents) {
            if (newEvents != null) {
                for (EventBean event : newEvents) {
                    System.out.println("Filtered Event: " + event.getUnderlying());
                }
            }
        }
    }
}
